package cn.com.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiT_member_partner;
import cn.com.api.entity.ApiU_service;
import cn.com.api.mapper.ApiT_memberMapper;
import cn.com.api.mapper.ApiT_member_partnerMapper;
import cn.com.api.mapper.ApiU_serviceMapper;
import cn.com.api.service.ApiU_serviceService;
import cn.com.api.service.impl.base.U_serviceServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.PageInfo;
import cn.com.util.Utils;

/**
 * 
 **/
@Service
public class ApiU_serviceServiceImpl extends U_serviceServiceImplBase implements ApiU_serviceService {
	@Autowired
	private ApiU_serviceMapper apiU_serviceMapper;
	@Autowired
	private ApiT_member_partnerMapper apiT_member_partnerMapper;

	public Map<String, Object> addService(ApiU_service apiU_service) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		apiU_service.setStatus(0);
		apiU_service.setAdd_time(DateUtil.nowTimeToString());
		apiU_serviceMapper.U_serviceInsertion(apiU_service);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作成功");
		map.put(Utils.DATAS, apiU_service);
		return map;
	}

	public Map<String, Object> cancelService(String service_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		ApiU_service apiU_service = apiU_serviceMapper.findU_serviceById(Integer.valueOf(service_id));
		if (apiU_service.getStatus() == 0) { // 可以取消，其他状态不可以取消
			apiU_service.setStatus(4);
			apiU_serviceMapper.updateU_service(apiU_service);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "取消成功");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "无法取消");
		}
		return map;
	}

	public Map<String, Object> confirmService(String service_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		ApiU_service apiU_service = apiU_serviceMapper.findU_serviceById(Integer.valueOf(service_id));
		if (apiU_service.getStatus() == 1) { // 1-合伙人已确定 才可以确认完成，其他状态不可以确认完成
			apiU_service.setStatus(2);
			apiU_service.setConfirm_time(DateUtil.nowTimeToString());
			apiU_serviceMapper.updateU_service(apiU_service);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "确认成功");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "无法无法确认");
		}
		return map;
	}

	public Map<String, Object> myServiceList(String user_id, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		List<ApiU_service> list = apiU_serviceMapper.findServicesListPage(map, pageInfo);
		map.clear();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作成功");
		map.put(Utils.DATAS, list);
		map.put(Utils.Page, pageInfo);
		return map;
	}

	public Map<String, Object> receiveService(String user_id, String service_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		ApiT_member_partner partner = apiT_member_partnerMapper.findTmemberpartnersByMap(map);
		if (partner != null && partner.getStatus() == 1) {
			ApiU_service service = apiU_serviceMapper.findU_serviceById(Integer.valueOf(service_id));
			service.setPartner(partner.getId());
			service.setStatus(1);
			apiU_serviceMapper.updateU_service(service);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "不是核伙人，无法操作");
		}
		return map;
	}

	public Map<String, Object> searchService(String user_id, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("excludeu_ser_id", user_id);
		List<ApiU_service> list = apiU_serviceMapper.findServicesListPage(map, pageInfo);
		map.clear();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作成功");
		map.put(Utils.DATAS, list);
		map.put(Utils.Page, pageInfo);
		return map;
	}

}
